package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;

import java.io.File;

/**
 * 图片上传适配器
 */
public final class ImageUploadAdapter extends AppAdapter<String> {

    private final int mMaxCount;
    private final OnDeleteListener mDeleteListener;
    private OnAddListener mAddListener;
    private int mPlaceholderResId = R.drawable.icon_camera;  // 默认占位图
    private boolean mShowPlaceholderText = false;  // 是否显示占位文字
    private String mPlaceholderTextPrefix = "上传图片";  // 占位文字前缀

    public ImageUploadAdapter(Context context, int maxCount, OnDeleteListener listener) {
        super(context);
        this.mMaxCount = maxCount;
        this.mDeleteListener = listener;
    }

    /**
     * 设置占位图资源ID
     */
    public void setPlaceholderResId(int resId) {
        this.mPlaceholderResId = resId;
    }

    /**
     * 设置添加图片监听器
     */
    public void setOnAddListener(OnAddListener listener) {
        this.mAddListener = listener;
    }

    /**
     * 设置是否显示占位文字（如"上传图片(5)"）
     */
    public void setShowPlaceholderText(boolean show) {
        this.mShowPlaceholderText = show;
    }

    /**
     * 设置占位文字前缀（默认"上传图片"）
     */
    public void setPlaceholderTextPrefix(String prefix) {
        this.mPlaceholderTextPrefix = prefix;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    /**
     * 是否显示添加按钮
     */
    public boolean shouldShowAddButton() {
        return getCount() < mMaxCount;
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView ivPlaceholder;  // 占位背景
        private final ImageView ivImage;  // 实际图片
        private final TextView tvPlaceholderText;  // 占位文字
        private final ImageView ivDelete;  // 删除按钮

        ViewHolder() {
            super(R.layout.image_upload_item);
            ivPlaceholder = findViewById(R.id.iv_placeholder);
            ivImage = findViewById(R.id.iv_image);
            tvPlaceholderText = findViewById(R.id.tv_placeholder_text);
            ivDelete = findViewById(R.id.iv_delete);
        }

        @Override
        public void onBindView(int position) {
            String imagePath = getItem(position);

            // 判断是否是相机图标
            boolean isCamera = "camera".equals(imagePath);

            if (isCamera) {
                // 显示占位状态
                ivPlaceholder.setVisibility(View.VISIBLE);
                ivPlaceholder.setImageResource(mPlaceholderResId);
                ivImage.setVisibility(View.GONE);
                ivDelete.setVisibility(View.GONE);

                // 显示占位文字（如"上传图片(5)"）
                if (mShowPlaceholderText) {
                    // 计算剩余可上传数量：最大数量 - 已上传数量
                    int uploadedCount = getCount() - 1;  // 减1是因为当前这个是占位符
                    int remainingCount = mMaxCount - uploadedCount;
                    tvPlaceholderText.setText(mPlaceholderTextPrefix + "(" + remainingCount + ")");
                    tvPlaceholderText.setVisibility(View.VISIBLE);
                } else {
                    tvPlaceholderText.setVisibility(View.GONE);
                }

                // 点击添加图片
                getItemView().setOnClickListener(v -> {
                    if (mAddListener != null) {
                        mAddListener.onAdd();
                    }
                });
            } else {
                // 显示实际图片状态
                ivPlaceholder.setVisibility(View.GONE);
                ivImage.setVisibility(View.VISIBLE);
                tvPlaceholderText.setVisibility(View.GONE);
                ivDelete.setVisibility(View.VISIBLE);

                // 加载图片
                Glide.with(getContext())
                        .load(new File(imagePath))
                        .placeholder(mPlaceholderResId)
                        .error(mPlaceholderResId)
                        .into(ivImage);

                // 删除按钮点击事件
                ivDelete.setOnClickListener(v -> {
                    if (mDeleteListener != null) {
                        mDeleteListener.onDelete(position);
                    }
                });

                // 清除添加点击事件
                getItemView().setOnClickListener(null);
            }
        }
    }

    /**
     * 删除监听器
     */
    public interface OnDeleteListener {
        void onDelete(int position);
    }

    /**
     * 添加监听器
     */
    public interface OnAddListener {
        void onAdd();
    }
}

